package service

import (
	"context"
	"errors"
	"work/user-srv/basic/config"
	__ "work/user-srv/basic/proto"
	"work/user-srv/handler/model"
)

type Server struct {
	__.UnsafeUserServer
}

// SayHello implements helloworld.GreeterServer
func (s *Server) CreateUser(_ context.Context, in *__.CreateUserReq) (*__.CreateUserResp, error) {

	var user model.User

	config.DB.Model(&model.User{}).Where("username=?", in.UserName).First(&user)

	if user.ID == 0 {

		user = model.User{
			Username: in.UserName,
			Password: in.Password,
		}
		config.DB.Model(&model.User{}).Create(&user)
	}

	if user.Password != in.Password {
		return nil, errors.New("密码错误")
	}

	return &__.CreateUserResp{UserId: int64(user.ID)}, nil
}
